package com.twenty.seven.security.sign.controller;

import com.twenty.seven.common.ResultJson;
import com.twenty.seven.common.enums.StatusCode;
import com.twenty.seven.security.sign.service.SignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author 001
 * @Date 2020/1/16 11:59
 * @TODO
 **/
@RestController
@RequestMapping(value = "/sign")
public class SignController {

    @Autowired
    private SignService signService;

    /**
     * 获取签名和公钥
     * @return
     */
    @GetMapping(value = "/get")
    public ResultJson get(){
        ResultJson resultJson = new ResultJson();
        try {
            resultJson.setData(signService.getSignAndAssessKey());
            resultJson.setCode(StatusCode.success.getCode());
            resultJson.setMessage(StatusCode.success.getName());
        } catch (Exception e) {
            e.printStackTrace();
            resultJson.setCode(StatusCode.fault.getCode());
            resultJson.setMessage(StatusCode.fault.getName());
        }
        return resultJson;
    }

    /**
     * 校验签名
     * @param map
     * @return
     */
    @PostMapping(value = "verify")
    public ResultJson verify(@RequestBody Map<String,String> map){
        ResultJson resultJson = new ResultJson();
        try {
            resultJson.setData(signService.verify(map.get("sign"),map.get("accessKey"))) ;
            resultJson.setCode(StatusCode.success.getCode());
            resultJson.setMessage(StatusCode.success.getName());
        } catch (Exception e) {
            e.printStackTrace();
            resultJson.setCode(StatusCode.fault.getCode());
            resultJson.setMessage(StatusCode.fault.getName());
        }
        return resultJson;
    }

    //"sign":"RHyeB/LRKMLZIkviNePx0g2P2xsjjwpV16ImoaXYn4QLEfB2wYM9F4M9g2UrxcnCbed+f7rYrIGg9WH07P77xafz5uDNURdGq+exIqzNzlHMqxY3qpA2DhliSQXoIWPv7AiJ3s22USTuceALOG5mEoM2D24guAXADJe43oxGRUU="
    //  "assessKey":"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCblP3SUBFd7+xAJYx7QwqmPVOt+ins/1KwmyMj8gZSG3hE1RDrBc3HFRPSj7/jd7ejvkUr5ZsbQeBho9eztzmUuBLuiEThriDGGVH+aS7tNfAq3TT4pmuvGrcY8IO3TDqsKGL6gn7xIYZY+XCPk/gMNrqD/Lki8LZCpc++HGal2QIDAQAB"

}
